﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link href="@ST.GetTrendsFile("My97DatePicker/skin/WdatePicker.css")" rel="stylesheet" />
<script src="@ST.GetTrendsFile("My97DatePicker/WdatePicker.js")"></script>
<script src="@ST.GetStaticFile("Static/Js/geo.js")"></script>
<div class="right">
    <h3>粉丝管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                <p><span>微信昵称：</span><input type="text" id="nickname"></p>
                <p>
                    <span>关注时间：</span>
                    <input type="date" class="manage_register_text" id="subscribe1" onfocus="WdatePicker()"><span class="manage_register_font">至</span><input id="subscribe2" type="date" class="manage_register_text" onfocus="WdatePicker()">
                </p>
            </div>
            <div class="manage_register">
                <p>
                    <span>性别：</span><select id="sex">
                        <option value="-1">全部</option>
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </p>
                <p>
                    <span>微信所在地：</span>
                    <select class="manage_register_text" name="province" id="s1" onchange="changeselect1(this.value)">
                        <option value="">请选择</option>
                        <option value="北京">北京</option>
                        <option value="上海">上海</option>
                        <option value="天津">天津</option>
                        <option value="重庆">重庆</option>
                        <option value="河北">河北</option>
                        <option value="山西">山西</option>
                        <option value="辽宁">辽宁</option>
                        <option value="吉林">吉林</option>
                        <option value="黑龙江">黑龙江</option>
                        <option value="江苏">江苏</option>
                        <option value="浙江">浙江</option>
                        <option value="安徽">安徽</option>
                        <option value="福建">福建</option>
                        <option value="江西">江西</option>
                        <option value="山东">山东</option>
                        <option value="河南">河南</option>
                        <option value="湖北">湖北</option>
                        <option value="湖南">湖南</option>
                        <option value="广东">广东</option>
                        <option value="甘肃">甘肃</option>
                        <option value="陕西">陕西</option>
                        <option value="内蒙古">内蒙古</option>
                        <option value="广西">广西</option>
                        <option value="四川">四川</option>
                        <option value="贵州">贵州</option>
                        <option value="云南">云南</option>
                        <option value="西藏">西藏</option>
                        <option value="海南">海南</option>
                        <option value="宁夏">宁夏</option>
                        <option value="青海">青海</option>
                        <option value="新疆">新疆</option>
                        <option value="香港">香港</option>
                        <option value="澳门">澳门</option>
                        <option value="台湾">台湾</option>
                    </select>
                    <span class="manage_register_font"></span>
                    <select class="manage_register_text" name="city" id="s2">
                    </select>
                </p>
            </div>
            <div class="manage_register">
                <p>
                    <span>分组：</span><span id="spGroupID"></span>
                </p>
                <p>
                    <span>状态：</span><span>
                        <select id="subStatu">
                            <option value="1">正在关注</option>
                            <option value="0">取消关注</option>
                        </select>
                    </span>
                </p>
            </div>
            <div class="manage_register">
                <div class="manage_register_user">
                    <span class="manage_register_user_left">用户标签：</span>
                    <div class="manage_register_user_right">
                        <span id="spUserLabel"></span>
                    </div>
                </div>
            </div>
            <p class="manage_add">
                <a id="btnQuery" style="cursor: pointer">查询</a>
                <a id="btnRet">重置</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <!--manage_bottom_title-->
            <div class="manage_bottom_title">
                <p>关注人数合计：<span class="red" id="gzCount"></span>人    累积关注人数：<span class="red" id="gzTotalCount"></span>人    取消关注人数：<span class="red" id="cancelCount"></span>人</p>
                @* <p class="manage_bottom_title_right">
                    <span id="spGroupID2"></span>
                    <a id="btnChange">更改分组</a>
                </p>*@
            </div>
            <!--manage_bottom_title end-->
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>
                        <input type="checkbox" id="cbSelected"></span></th>
                    <th><span>编号</span></th>
                    <th><span>微信昵称</span></th>
                    <th><span>Open ID</span></th>
                    <th><span>关注时间</span></th>
                    <th><span>取消关注时间</span></th>
                    <th><span>性别</span></th>
                    <th><span>省份</span></th>
                    <th><span>城市</span></th>
                    <th><span>用户标签</span></th>
                    <th><span>社区用户</span></th>
                    <th><span>手机号码</span></th>
                    <th><span>备注</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
            <p class="manage_page_font">共 <span id="divRecord"></span>条记录</p>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<div style="width: 100%; height: 100%; display: none; position: absolute; top: 0px; left: 0px;" id="loadDiv">
    <div id="subLoadDiv">
        <img src="@Url.Content("~/Static/Image/load.gif")" />
    </div>
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        createMemu();
        //更改组操作
        $("#btnChange").click(function () {
            var groupid2 = $("#groupid2").val();
            if (groupid2 == "" || groupid2 == "-1") {
                alert("请选择分组！");
                $("#groupid2").focus();
                return;
            }
            var ids = getCurrPageids().split('~');
            var wxids = ids[0];
            var openids = ids[1];
            if (ids == "") {
                alert("请选择要分组的用户！");
                return;
            }
            operate(wxids, openids, groupid2);
        });
        //查询
        $("#btnQuery").click(function () {
            pageIndex = 1;
            pageSize = 10;
            pageCount = 1;
            totalCount = 0;
            gotoPage();
        });
        //取消
        $("#btnRet").click(function () {
            reseting();
        });
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            statsubscribe();
            gotoPage();

            //上面一个绑定组
            bindDrpList("@ST.GetTrendsFile("WXUser/QueryUserGroupList")", "spGroupID", "groupid", "id", "name", function () { });
            //下面一个绑定组
            bindDrpList("@ST.GetTrendsFile("WXUser/QueryUserGroupList")", "spGroupID2", "groupid2", "id", "name", function () { });
            //绑定标签
            bindCheckBoxList("@ST.GetTrendsFile("WXUser/QueryUserLabelList")", "spUserLabel", "labelid", "id", "name", function () { });

            getCurrPageSeletedOperate();
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }


    function showLoading() {
        var margintop = (document.documentElement.clientHeight) / 2 - 20;
        var marginleft = (document.documentElement.clientWidth) / 2 - 20;
        $("#subLoadDiv").css({ "margin-top": margintop, "margin-left": marginleft });
        $("#loadDiv").show();
    }

    function hideLoading() {
        $("#loadDiv").hide();
    }

    //获取所有标签labelids
    function getlabelids() {
        var labelids = "";
        $("input[name='labelid']").each(function () {
            if ($(this).attr("checked") == "checked") {
                var d = $(this).val() + ",";
                labelids += d;
            }
        });
        return labelids.trim(',');
    }
    //获取当前页主键ids
    function getCurrPageids() {
        var ids = "";
        var openids = "";
        $("#dvList input[type='checkbox']").each(function (i, n) {
            if ($(this).attr("checked") == "checked") {
                var d = $(this).attr("wxid") + ",";
                var d1 = $(this).attr("openid") + ",";
                ids += d;
                openids += d1;
            }
        });
        return ids.trim(',') + "~" + openids.trim(',');
    }

    //选中当前页的全部按钮操作事件
    function getCurrPageSeletedOperate() {
        $("#cbSelected").change(function () {
            if ($(this).attr("checked") == "checked") {
                $("#dvList input[type='checkbox']").each(function (i, n) {
                    $(this).attr("checked", true);
                });
            }
            else {
                $("#dvList input[type='checkbox']").each(function (i, n) {
                    $(this).attr("checked", false);
                });
            }
        });
    }
    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        ajaxdata["nickname"] = $("#nickname").val();
        ajaxdata["sex"] = $("#sex").val();
        ajaxdata["province"] = $("#s1").val();
        ajaxdata["city"] = $("#s2").val();
        ajaxdata["subscribe1"] = $("#subscribe1").val();
        ajaxdata["subscribe2"] = $("#subscribe2").val();
        ajaxdata["subStatu"] = $("#subStatu").val();
        ajaxdata["groupid"] = $("#groupid").val();
        ajaxdata["labelids"] = getlabelids();
        ajaxdata["weixinid"] = weixinid;
        $.ajax({
            url: "@ST.GetTrendsFile("WXUser/QueryWXUserList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            dataType: 'json',
            beforeSend: function () {
                showLoading();
            },
            complete: function () {
                hideLoading();
            },
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    totalCount = result.total;;
                    var d = result.list;
                    var html = "";
                    if (d != null && d != "") {
                        for (var i = 0; i < d.length; i++) {
                            html += "<tr>";
                            html += getCheckBoxTd(d[i]["wxid"], d[i]["openid"]) + getTxtTd(d[i]["rowid"]) + getTxtTd(d[i]["nickname"]) + getTxtTd(d[i]["openid"]) + getTxtTd(d[i]["subscribe_time"].replace(/T/g, " "));
                            html += getTxtTd(d[i]["subscribe_time"].replace(/T/g, " ")) + getTxtTd(d[i]["sexname"]) + getTxtTd(d[i]["province"]) + getTxtTd(d[i]["city"]);
                            html += getTxtTd(d[i]["labelname"]) + getTxtTd(d[i]["issq"]) + getTxtTd(d[i]["tel"]) + getTxtTd(d[i]["remark"]);
                            html += '</tr>';
                        }
                    }
                    $("#dvList").html(html);
                    PageCountHtml();
                    $("#divRecord").html(totalCount);

                } else {
                    $("#dvList").html("没有查询到相关数据!");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getCheckBoxTd(wxid, openid) {
        return '<td><span><input type="checkbox" wxid="' + wxid + '" openid="' + openid + '"></span></td>';
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }

    //编辑操作
    function operate(wxids, openids, groupid2) {
        $.ajax({
            url: "@ST.GetTrendsFile("WXUser/UpdateWXUserGroup")",
            type: "POST",
            async: true,
            data: { wxids: wxids, openids: openids, groupid: groupid2, weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    reseting();
                    gotoPage();
                    alert("操作成功。");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#nickname").val("");
        $("#sex").val("-1");
        $("#province").val("-1");
        $("#city").val("-1");
        $("#subscribe1").val("");
        $("#subscribe2").val("");
        $("#groupid").val("-1");
    }
    function bindCheckBoxList(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: { weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '';
                    for (var i = 0; i < d.length; i++) {
                        html += ' <input type="checkbox" name="' + id + '" value="' + d[i][value] + '">' + d[i][name] + '</span>';
                    }

                    $("#" + spid).append(html);
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function bindDrpList(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: { weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '<select  id="' + id + '" ><option value="-1">全部</option>';
                    for (var i = 0; i < d.length; i++) {
                        html += ' <option value="' + d[i][value] + '">' + d[i][name] + '</option>';
                    }
                    html += '</select>';
                    $("#" + spid).append(html);
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }




    //统计
    function statsubscribe() {
        $.ajax({
            url: "@ST.GetTrendsFile("WXUser/GetWXUserStat")",
            type: "POST",
            async: true,
            data: { weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    var arrData = result.split("~");
                    var gzCount = arrData[0];
                    var cancelCount = arrData[1];
                    var gzTotalCount = arrData[2];
                    $("#gzCount").html(gzCount);
                    $("#cancelCount").html(cancelCount);
                    $("#gzTotalCount").html(gzTotalCount);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>
